
package com.hlkj.pay.service.merchant.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.List;

import com.hlkj.pay.app.common.ICommonAppService;
import com.hlkj.pay.config.SmsTemplateProperties;
import com.hlkj.pay.service.common.IOperatedLogService;
import com.hlkj.pay.vo.common.req.SmsSendReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.common.MerchantResultCode;
import com.hlkj.pay.dto.common.DefaultConfig;
import com.hlkj.pay.dto.merchant.MerchantInfoRegisterDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.model.merchant.DefaultFeeConfigDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantConfigDO;
import com.hlkj.pay.service.admin.IAdminUserService;
import com.hlkj.pay.service.common.IPlatFormConfigService;
import com.hlkj.pay.service.merchant.IMerchantConfigService;
import com.hlkj.pay.service.merchant.IMerchantDefaultConfigService;
import com.hlkj.pay.service.merchant.IMerchantService;

import lombok.RequiredArgsConstructor;

/**
 * @author HlpayTeam
 * @date 2024/11/08 15:45
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MerchantHandler {

    private final IMerchantService merchantService;

    private final IOperatedLogService merchantLogService;

    private final IMerchantConfigService merchantConfigService;

    private final IAdminUserService adminUserService;

    private final IPlatFormConfigService platFormConfigService;

    private final IMerchantDefaultConfigService merchantDefaultConfigService;

    private final SmsTemplateProperties smsTemplateProperties;

    private final ICommonAppService commonAppService;

    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> register(MerchantInfoRegisterDto merchantInfoRegisterDto, List<String> providerCodeList, List<String> channelCodeList) {
        CommonEnum.SYS_TYPE sysType = merchantInfoRegisterDto.getSysType();
        // if (CommonEnum.SYS_TYPE.MERCHANT.getCode().equals(sysType.getCode())) {
        // return CommonResult.error(MerchantResultCode.MERCHANT_REGISTER_NOT_EXIST);
        // }
        if (merchantInfoRegisterDto.getRegisterChannel() == null) {
            switch (sysType) {
                case AGENT:
                    merchantInfoRegisterDto.setRegisterChannel(3);
                    break;
                default:
                    merchantInfoRegisterDto.setRegisterChannel(1);
            }
        }
        merchantInfoRegisterDto.setSysType(sysType);
        // 1:商户 信息 商户app 生成
        merchantService.register(merchantInfoRegisterDto);
        String sn = merchantInfoRegisterDto.getSn();
        if (!StringUtils.hasText(sn)) {
            return CommonResult.error(MerchantResultCode.MERCHANT_REGISTER_ERROR);
        }

        merchantLogService.save(merchantInfoRegisterDto.getId(), MerchantEnum.MERCHANT_LOG_TYPE.MERCHANT, "新增商户信息", "新增商户");
        // 系统默认配置
        if (CollectionUtils.isEmpty(providerCodeList) || CollectionUtils.isEmpty(channelCodeList)) {
            DefaultConfig defaultConfig = platFormConfigService.getDefaultConfig();
            DefaultConfig.OtherConfig otherConfig = defaultConfig.getOtherConfig();
            providerCodeList = otherConfig.getProviderCodeList();
            channelCodeList = otherConfig.getChannelCodeList();
        }
        log.info("sysType:{},sn:{}", sysType.getCode(), sn);
        // 默认费率生成
        DefaultFeeConfigDO defaultFeeConfigDO = merchantDefaultConfigService.queryDetail(sysType.getCode(), sn);
        if (defaultFeeConfigDO == null) {
            defaultFeeConfigDO = merchantDefaultConfigService.queryDetail(MerchantEnum.MERCHANT_SN_TYPE.MERCHANT.getCode(), null);
        }
        // 商户默认配置
        if (!CollectionUtils.isEmpty(providerCodeList) || !CollectionUtils.isEmpty(channelCodeList) || defaultFeeConfigDO != null) {
            MerchantConfigDO merchantConfigDO = new MerchantConfigDO();
            merchantConfigDO.setSn(sn);
            if (!CollectionUtils.isEmpty(channelCodeList)) {
                merchantConfigDO.setChannelCode(JsonUtils.toJsonString(channelCodeList));
            }
            if (!CollectionUtils.isEmpty(providerCodeList)) {
                merchantConfigDO.setProviderCode(JsonUtils.toJsonString(providerCodeList));
            }
            if (defaultFeeConfigDO != null && StringUtils.hasText(defaultFeeConfigDO.getFeeConfig())) {
                merchantConfigDO.setFeeConfig(defaultFeeConfigDO.getFeeConfig());
            }
            merchantConfigService.saveConfig(merchantConfigDO);
        }
        // 3 商户默认用户生成
        adminUserService.addMerchantUser(merchantInfoRegisterDto);

        //新增商户发送账号密码信息
        SmsSendReq smsSendReq = new SmsSendReq();
        smsSendReq.setAccount(merchantInfoRegisterDto.getAccount());
        smsSendReq.setCode(merchantInfoRegisterDto.getPwd());
        smsSendReq.setTemplateCode(smsTemplateProperties.getRegisterMerchantNotify());
        smsSendReq.setPhone(merchantInfoRegisterDto.getPhone());
        commonAppService.sendSms(smsSendReq);
        return CommonResult.success(sn);
    }
}
